﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using BlazorOrdering.Application;
using BlazorOrdering.Core;
using BootstrapBlazor.Components;
using Furion.DatabaseAccessor;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;

namespace BlazorOrdering.Web.Entry.Pages
{
    public partial class EditMeun 
    {
        private Message MessageElement { get; set; }
        [Inject] 
        public MessageService MessageService { get; set; }

        protected static IEnumerable<int> PageItemsSource => new int[] { 2, 4, 10, 20 };

        [NotNull]
        private List<BoMenu>? BoMeuns { get; set; }

        [Inject] 
        public IBoMenuService menuService { get; set; }


        public BoMenu CurrentItem { get; set; } = new();

        private IEnumerable<SelectedItem>? Types = new SelectedItem[]
      {
            new SelectedItem ("早餐", "早餐"),
            new SelectedItem ("午餐", "午餐"),
            new SelectedItem ("晚餐", "晚餐") ,
      };
        /// <summary>
        /// OnInitialized 方法
        /// </summary>
        protected override async Task OnInitializedAsync()
        {
            base.OnInitialized();
            BoMeuns = await menuService.GetAllMenusAsync(DateTime.Now.Date);// 获取菜谱
        }

        private Task<QueryData<BoMenu>> OnQueryAsync(QueryPageOptions options)
        {
            IEnumerable<BoMenu> items = BoMeuns;

            // 设置记录总数
            var total = items.Count();

            // 内存分页
            items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

            return Task.FromResult(new QueryData<BoMenu>()
            {
                Items = items,
                TotalCount = total,
                IsSorted = true,
                IsFiltered = true,
                IsSearch = true
            });
        }

        private Task ClickRow(BoMenu item)
        {
            CurrentItem = item;
            StateHasChanged();
            return Task.CompletedTask;
        }

        private Task OnInvalidSubmit(EditContext context)
        {
            ShowMessage("填写不符合规范，请检查");
            return Task.CompletedTask;
        }

        private async Task<Task> OnValidSubmit(EditContext context)
        {
            BoMenu bm = (BoMenu)context.Model;
            string msg = await menuService.Update(bm);
            if (msg=="ok")
            {

            ShowMessage("修改成功!");
            }
            else
            {
                ShowMessage(msg);
            }
            
            return Task.CompletedTask;
        }
        private void ShowMessage(string msg)
        {
            MessageElement.SetPlacement(Placement.Top);
            MessageService?.Show(new MessageOption()
            {
                Host = MessageElement,
                Content = msg
            });
        }

        protected async Task<Task> OnRowButtonClick(BoMenu menu)
        {
            //StateHasChanged();
            string result = await menuService.Del(menu);
            if (result=="ok")
            {
            ShowMessage(menu.Name+" 删除成功 ！");
                BoMeuns.Remove(menu);//从表格中删除行
                //BoMeuns = await menuService.GetAllMenusAsync(DateTime.Now.Date);// 重新填充表格
                //StateHasChanged();//表格不刷新，这样试一试

            }
            else
            {
                ShowMessage(result);
            }


            return Task.CompletedTask;
        }

    }


}
